package com.xuecheng.checkcode.config;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import com.xuecheng.checkcode.exception.SmsSendFailedException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 勾新杰
 * @version 1.0
 * @description: 阿里云短信发送端配置
 * @date 2025/2/25 10:35
 */
@Slf4j
@Configuration
public class SmsClientConfig {

    private static final String ACCESS_KEY_ID = "ALIBABA_CLOUD_ACCESS_KEY_ID";

    private static final String ACCESS_KEY_SECRET = "ALIBABA_CLOUD_ACCESS_KEY_SECRET";

    private static final String ENDPOINT = "dysmsapi.aliyuncs.com";

    @Bean
    public Client client() {
        try {
            Config config = new Config()
                    .setAccessKeyId(System.getenv(ACCESS_KEY_ID))
                    .setAccessKeySecret(System.getenv(ACCESS_KEY_SECRET));
            config.endpoint = ENDPOINT;
            return new Client(config);
        } catch (Exception e) {
            log.error("获取短信客户端失败，原因为{}", e.getMessage());
            throw new SmsSendFailedException();
        }
    }
}
